package com.ruoyi.project.cms.notice.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.notice.domain.NoticeType;
import com.ruoyi.project.cms.notice.service.NoticeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * com.ruoyi.project.cms.notice.controller
 * 作者：lh
 * 描述:
 * 时间:2020-05-17
 */
@Controller
@RequestMapping("/notice/type")
public class NoticeTypeController extends BaseController {

    private String prefix = "cms/notice/type";

    @Autowired
    private NoticeTypeService cNTService;

    @GetMapping("")
    public String type()
    {
        return prefix + "/type";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NoticeType cmsNoticeType)
    {
        startPage();
        List<NoticeType> list = cNTService.selectNoticeTypeList(cmsNoticeType);
        return getDataTable(list);
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @Log(title = "公告类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated NoticeType cmsNoticeType)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(cNTService.checkNoticeTypeNameUnique(cmsNoticeType)))
        {
            return error("新增公告类型'" + cmsNoticeType.getTname() + "'失败，公告类型已存在");
        }
        return toAjax(cNTService.insert(cmsNoticeType));
    }

    /**
     * 修改公告类型
     */
    @GetMapping("/edit/{tid}")
    public String edit(@PathVariable("tid") Integer tid,ModelMap mmap)
    {
        System.out.println("111111111111111111111111"+tid);

        mmap.put("type",cNTService.selectByPrimaryKey(tid));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @Log(title = "公告类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated NoticeType cmsNoticeType)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(cNTService.checkNoticeTypeNameUnique(cmsNoticeType)))
        {
            return error("修改公告类型'" + cmsNoticeType.getTname() + "'失败，公告类型已存在");
        }
        return toAjax(cNTService.updateByPrimaryKey(cmsNoticeType));
    }

    /**
     * 删除公告类型
     * @param ids
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(cNTService.deleteNoticeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 公告类型名校验
     * @param cmsNoticeType
     * @return
     */
    @PostMapping("/checkNoticeTypeNameUnique")
    @ResponseBody
    public String checkNoticeTypeNameUnique(NoticeType cmsNoticeType)
    {
        return cNTService.checkNoticeTypeNameUnique(cmsNoticeType);
    }
}
